package com.example.myapplication.tack_22;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.myapplication.R;
import com.example.myapplication.tack_22.adapter.RouteAdapter;
import com.example.myapplication.tack_22.data.RouteData;
import com.example.myapplication.utils.OkHttpTool;

public class MetroActivity extends AppCompatActivity {
    private ImageView exit;
    private RecyclerView route;
    private LinearLayout line;
    private ImageView map;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tack_22_activity_metro);
        init();
        exit_();
        getRoute();
        line_();
        map_();
    }

    private void map_() {
        map.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MetroActivity.this,AllRoutesActivity.class);
                MetroActivity.this.startActivity(intent);
            }
        });
    }

    private void line_() {
        line.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MetroActivity.this,OverviewMapActivity.class);
                MetroActivity.this.startActivity(intent);
            }
        });
    }

    private void getRoute() {
        String url = "prod-api/api/metro/list?currentName=建国门";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, RouteData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                RouteData routeData = (RouteData)obj;
                if(routeData.getCode() == 200){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            route.setAdapter(new RouteAdapter(routeData.getData(),MetroActivity.this));
                            route.setLayoutManager(new LinearLayoutManager(MetroActivity.this));
                        }
                    });
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void exit_() {
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MetroActivity.this.finish();
            }
        });
    }

    private void init() {
        exit = findViewById(R.id.exit);
        route = findViewById(R.id.route);
        line = findViewById(R.id.line);
        map = findViewById(R.id.map);
    }
}